import OSS from 'ali-oss';
// const OSS = require("ali-oss");
const cdnUrl = "https://zmjcdn.fmkjfile.com/";
const cdnPrefix = "companyproject/zt-test";
//获取随机id
const getFileNameUUID = () => {
  function rx() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  }
  return `${+new Date()}_${rx()}${rx()}`;
};
const client = new OSS({
  region: "oss-cn-shenzhen", // 服务器集群地区
  accessKeyId: "LTAI4GBZESKaNYNX67eYgmAy", // OSS帐号
  accessKeySecret: "YGVINxEGyYYGhcB03wJlRFU2MedsLN", // OSS 密码
  bucket: "applets-zhongtai", // 阿里云上存储的 Bucket
});
const aliUpload = (file): Promise<string | null> => {
  console.log(file,'file');
  
  //获取文件名后缀
  const extArr: any = file.type.split("/");
  const extName = extArr[extArr.length - 1];

  let objName = getFileNameUUID();
  let key = `/${cdnPrefix}/${extName}/${objName}.${extName}`; // 存储路径，并且给图片改成唯一名字

  return new Promise((resolve, reject) => {
    client
      .multipartUpload(key, file)
      .then(() => {
        resolve(cdnUrl + key);
      })
      .catch(() => {
        reject();
      });
  });
};
export default aliUpload;
